<?php

    /**
     * @package         Captcha
     * @subpackage      Adapter
     *
     * @license         GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    abstract class Captcha_Adapter_Abstract
    {

    /////////////////////////////////////////////////////////////////////////////
    //                               Attributes                                //
    /////////////////////////////////////////////////////////////////////////////

        /**
         * Background image
         * 
         * @var     string
         */
        protected $_background = null;

        /**
         * Image type
         *
         * @var     string
         */
        protected $_type = null;

        /**
         * Default image width
         *
         * @var     int
         */
        protected $_width = 175;

        /**
         * Default image height
         *
         * @var     int
         */
        protected $_height = 45;

        /**
         * Code length
         *
         * @var     int
         */
        protected $_length = 4;

        /**
         * Available characters
         *
         * @var     string
         */
        protected $_charset = 'ABCDEFGHKLMNPRSTUVWYZ23456789';

        /**
         * TTF file to use
         *
         * @var     string
         */
        protected $_ttf = null;

    /////////////////////////////////////////////////////////////////////////////
    //                                Constants                                //
    /////////////////////////////////////////////////////////////////////////////

        const IMAGE_JPEG    = 'jpeg';
        const IMAGE_GIF     = 'gif';
        const IMAGE_PNG     = 'png';

    /////////////////////////////////////////////////////////////////////////////
    //                               Methods                                   //
    /////////////////////////////////////////////////////////////////////////////

        /**
         * Sets background image
         *
         * @throws      Core_IO_FileNotFoundException
         * @param       string      $background
         * @return      void
         */
        public function setBackground($background)
        {
            if (!Core_IO_File::exists($background)) {
                throw new Core_IO_FileNotFoundException('Fatal error : file '. $background .' does not exist');
            }

            $this->_background = $background;
        }

        /**
         * Sets image type
         *
         * @param       string      $type
         * @return      void
         */
        public function setImageType($type)
        {
            $this->_type = $type;
        }

        /**
         * Sets image width
         *
         * @throws      Core_ArgumentException
         * @param       int         $width
         * @return      void
         */
        public function setWidth($width)
        {
            if (!is_int($width)) {
                throw new Core_ArgumentException('Fatal error : width must be an integer');
            }

            $this->_width = $width;
        }

        /**
         * Sets image height
         *
         * @throws      Core_ArgumentException
         * @param       int         $height
         * @return      void
         */
        public function setHeight($height)
        {
            if (!is_int($height)) {
                throw new Core_ArgumentException('Fatal error : height must be an integer');
            }

            $this->_height = $height;
        }

        /**
         * Sets code length
         *
         * @throws      Core_ArgumentException
         * @param       int         $length
         * @return      void
         */
        public function setLength($length)
        {
            if (!is_int($length)) {
                throw new Core_ArgumentException('Fatal error : length must be an integer');
            }

            $this->_length = $length;
        }

        /**
         * Sets charset
         *
         * @throws      Core_ArgumentException
         * @param       string      $charset
         * @return      void
         */
        public function setCharset($charset)
        {
            if (!is_string($charset)) {
                throw new Core_ArgumentException('Fatal error : charset must be a string');
            }

            $this->_charset = $charset;
        }

        /**
         * Sets ttf file
         *
         * @throws      Core_IO_FileNotFoundException
         * @param       string      $ttf
         * @return      void
         */
        public function setTtf($ttf)
        {
            if (!Core_IO_File::exists($ttf)) {
                throw new Core_IO_FileNotFoundException('Fatal error : file '. $ttf .' does not exist');
            }

            $this->_ttf = $ttf;
        }

    /////////////////////////////////////////////////////////////////////////////
    //                                Static methods                           //
    /////////////////////////////////////////////////////////////////////////////

        /**
         * Generates unique id to prevent IE from caching
         *
         * @return      string
         */
        public static function uniqueId()
        {
            return md5(uniqid(time()));
        }

    /////////////////////////////////////////////////////////////////////////////
    //                            Abstract Methods                             //
    /////////////////////////////////////////////////////////////////////////////

        /**
         * Shows
         *
         * @return      string
         */
        abstract public function show();

        /**
         * Checks result
         *
         * @param       string      $code
         * @return      boolean
         */
        abstract public function check($code);
    }